<?php defined('SYSPATH') or die('No direct script access.');

class Model_Shop_Category extends ORM
{ 
    protected $_reload_on_wakeup = false; 
    
    public function add_category($category_id, $name, $parent_id = 0)
    {
        // TODO validation
        
        $this->source_id = $category_id;
        if ($parent_id) {
            $this->parent_id = $parent_id;
        }
        $this->shop_id = ORM::factory('shop')->get_current()->id;
        $this->name = $name;
        $this->save();
        
        return $this;
    }
    
    public function get_top_categories()
    {
        return $this->_get_categories_query()->where('parent_id', '=', 0)->find_all(); 
    } 
    
    public function get_categories()
    {
        return $this->_get_categories_query()->where('parent_id', '=', $this->source_id)->find_all();  
    }
    
    public function get_category($category_id)
    {
        $category = $this->_get_categories_query()->where('source_id', '=', $category_id)->find();
        if ($category->loaded()) {
            return $category;
        }
        return null;
    }
    
    protected function _get_categories_query()
    {
        return ORM::factory('shop_category')
            ->where('shop_id', '=', ORM::factory('shop')->get_current()->id);  
    }
    
    public function get_breadcrumbs()
    {
        $breadcrumbs = array();
        $category_id = $this->parent_id;
        $old_category_id = $category_id;
        
        while ($category_id) {
            //$category = ORM::factory('category', $category_id);
            $category = $this->_get_categories_query()
                            ->where('source_id', '=', $category_id)
                            ->limit(1)
                            ->find();
            $breadcrumbs[] = $category;   
            
            $category_id = $category->parent_id;
        }
        
        $breadcrumbs = array_reverse($breadcrumbs);
        return $breadcrumbs;    
    }
    
    /*public function extract_categories($products)
    {
        if ($products instanceof Model_Product) {
            $product = $products;
            $categories = array();
            if ($product->category_id) {
                $categories[$product->category_id] = ORM::factory('category', $product->category_id);
            }  
        } else if (is_array(Model_Main::get_array($products))) {
            $categories = array();
            foreach ($products as $product) {
                if (!in_array($product->category_id, $categories) && $product->category_id) {
                    $categories[] = $product->category_id;
                }    
            }
            if (count($categories)) {           
                $categories = $this->_get_categories_query()->where('id', 'IN', $categories)->find_all();
                $categories = Model_Main::process_array($categories); 
            }            
        }
        return $categories;
    }    */
}